package fr.zadoka.zadokadna.Loader;

import java.util.GregorianCalendar;

import fr.zadoka.zadokadna.Connectivity.DailyImageLoader;
import fr.zadoka.zadokadna.Connectivity.UrlImageLoader;
import fr.zadoka.zadokadna.Util.CalendarUtil;
import android.graphics.Bitmap;

public class ZadokaImageLoader {

	public Bitmap getImage(GregorianCalendar date)
	{
		return getImage(date, new ZadokaImageLoaderFactory());
	}
	
	public Bitmap getImage(GregorianCalendar date, ZadokaImageLoaderFactory loaderFactory)
	{
		UrlImageLoader loader = loaderFactory.getLoader(date);
		return getImage(date, loader);
	}
	
	public Bitmap getImage(GregorianCalendar date, UrlImageLoader loader)
	{
		if (loader instanceof DailyImageLoader)
			return ((DailyImageLoader)loader).getImage();
		else
			return loader.getImage(getUrlFromDate(date));		
		
	}
	
	public String getUrlFromDate(GregorianCalendar date)
	{
		//format the string
        StringBuilder urlBuilder = new StringBuilder("http://www.zadoka.fr/zadoka/daily_zadoka_sent/");

        //add the dir
		String dateString = CalendarUtil.GregorianCalendarToString(date);
		urlBuilder.append(dateString);
		urlBuilder.append("/");
			
		//add the filename
		urlBuilder.append(dateString);
		urlBuilder.append("_picture.jpg");	
		
		return urlBuilder.toString();
	}
}
